package com.yuyou.fn.activity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.StringUtil;
import com.google.common.collect.Lists;
import com.yuyou.fn.activity.service.IActivityCampusService;
import com.yuyou.fn.activity.service.IActivityTargetService;
import com.yuyou.fn.activity.vo.ActivityDetailVo;
import com.yuyou.fn.activity.vo.ActivityQueryVo;
import com.yuyou.fn.activity.vo.ActivitySaveUpdateVo;
import com.yuyou.fn.activity.vo.InputVo;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.activity.mapper.ActivityMapper;
import com.yuyou.fn.activity.entity.Activity;
import com.yuyou.fn.activity.service.IActivityService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 * Activity 表数据服务层接口实现类
 *
 */
@Service
public class ActivityServiceImpl extends SuperServiceImpl<ActivityMapper, Activity,Long> implements IActivityService {

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IActivityTargetService activityTargetService;

    @Resource
    private IActivityCampusService activityCampusService;

    @Override
    @Transactional
    public void saveOrUpdateActivityVo(ActivitySaveUpdateVo activitySaveUpdateVo) {
        List<InputVo> inputVoList=activitySaveUpdateVo.getFields();
        if (!Utils.isEmpty(inputVoList)){
            List<String> titles = BeanUtils.toList(inputVoList, "title");
            List list = Utils.removeDuplicate(titles);
            if (titles.size()!=list.size()){
                throw new BusException("字段名重复");
            }
        }
        Activity activity=new Activity();
        BeanUtils.copyProperties(activity,activitySaveUpdateVo);
        activity.setShowFields(JSONObject.toJSONString(activitySaveUpdateVo.getFields()));
        activity.setUpdateTime(TimeUtils.now());
        if (activity.getActivityId()==null){
            activity.setCreateTime(TimeUtils.now());
            this.insertSelective(activity);
        }else {
            activityCampusService.updateActivityNameAndStartDateByActivityId(activitySaveUpdateVo.getActivityId(),activitySaveUpdateVo.getActivityName(),activitySaveUpdateVo.getActivityStartDate());
            activityTargetService.updateActivityNameAndStartDateByActivityId(activitySaveUpdateVo.getActivityId(),activitySaveUpdateVo.getActivityName(),activitySaveUpdateVo.getActivityStartDate());
            this.updateSelectiveById(activity);
        }
    }

    @Override
    public Page<ActivityDetailVo> findActivityDetailPage(ActivityQueryVo activityQueryVo,int pageNo,int pageSize) {
        Map<Long,AreaTeam> areaTeamMap=areaTeamService.findAreaTeamMap();
        Map<Long,Period> periodMap=periodService.findPeriodMapByAreaTeamId(activityQueryVo.getAreaTeamId());

        Example<Activity> example=Activity.createExample();

        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,activityQueryVo);
        example.orderBy("createTime").desc();
        Page<Activity> activityPage = this.findPageByExample(example,pageNo,pageSize);
        Page<ActivityDetailVo> activityDetailVoPage= Utils.newPage(activityPage);
        List<Activity> activityList=activityPage.getRecords();
        List<ActivityDetailVo> activityDetailVoList = Lists.transform(activityList, item -> {
            ActivityDetailVo activityDetailVo=new ActivityDetailVo();
            BeanUtils.copyProperties(activityDetailVo,item);
            activityDetailVo.setAreaTeamName(areaTeamMap.get(item.getAreaTeamId()).getName());
            activityDetailVo.setPeriodName(periodMap.get(item.getPeriodId()).getPeriodName());
            return activityDetailVo;
        });
        activityDetailVoPage.setRecords(activityDetailVoList);
        return activityDetailVoPage;
    }

    @Override
    public ActivityDetailVo findActivityDetailById(long activityId) {
        Activity activity=this.findById(activityId);
        AreaTeam areaTeam=areaTeamService.findById(activity.getAreaTeamId());
        Period period=periodService.findById(activity.getPeriodId());
        ActivityDetailVo activityDetailVo=new ActivityDetailVo();
        BeanUtils.copyProperties(activityDetailVo,activity);
        activityDetailVo.setAreaTeamName(areaTeam.getName());
        activityDetailVo.setPeriodName(period.getPeriodName());
        return activityDetailVo;
    }

    @Override
    public List<Activity> findActivityListByPeriodId(long periodId) {
        Example<Activity> example=Activity.createExample();
        example.createCriteria().andEqualTo("periodId",periodId);
        return this.findListByExample(example);
    }
}